home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / qndxr.2 ƒ / bufio.2.c next >
Encoding:
C/C++ Source or Header  |  1987-11-03  |  4.3 KB  |  179 lines  |  [TEXT/KAHL]

  1. /* This is the file 'bufio.c' ... by ^z, 870830-0913- ... it includes 
  2.  * definitions of some buffer words to accumulate information on its
  3.  * way to/from the disks ... use these to speed up operations and reduce
  4.  * disk head movements, in place of calls to fputc(), fread(), fwrite(),
  5.  * etc. ... try to make them very general so that they will simplify
  6.  * other tasks....
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <unix.h>
  12. #include <storage.h>
  13. #include <strings.h>
  14. #include <ctype.h>
  15. #include <proto.h>
  16. #include "qndxr.2.h"
  17.  
  18. /* set up some buffers here to save on disk head movement and speed up
  19.  * operations ... use my simple ZBUF structure for both input and
  20.  * output operations ... keep everything static, local here to this file
  21.  * for safety's sake ... allocate enough items here for all the buffers
  22.  * that we may need for an NMERGE-way merge operation, taking into account
  23.  * the need for input buffers for all the NMERGE *.k and *.p files plus
  24.  * the output files *.k and *.p as well....
  25.  */
  26.  
  27. static ZBUF zbuffer[2 + 2 * NMERGE];
  28.  
  29.  
  30. /* function to create a zbuffer and set its parameters appropriately
  31.  */
  32.  
  33. void create_zbuffer (n, bufsize, buffile, bufitemsize)
  34.   int n, bufitemsize;
  35.   long bufsize;
  36.   FILE *buffile;
  37.   {
  38.     char *make_buf();
  39.     
  40.     zbuffer[n].zbufbase = make_buf (bufsize);
  41.     zbuffer[n].zbufp = zbuffer[n].zbufbase;
  42.     zbuffer[n].zbufcounter = 0;
  43.     zbuffer[n].zbufsize = bufsize;
  44.     zbuffer[n].zbuffilep = buffile;
  45.     zbuffer[n].zbufitemsize = bufitemsize;
  46.   }
  47.  
  48.  
  49. /* function to free a zbuffer ...
  50.  */
  51.  
  52. void free_zbuffer (n)
  53.   int n;
  54.   {
  55.     free (zbuffer[n].zbufbase);
  56.   }
  57.  
  58.  
  59. /* function to return a pointer to the next item in a chosen input
  60.  * buffer 'n'; it reloads the buffer if necessary ... returns NULL
  61.  * pointer when there's nothing left in the file ...
  62.  */
  63.  
  64. char *next_input_item (n)
  65.   int n;
  66.   {
  67.     char *result;
  68.     
  69.     if (zbuffer[n].zbufcounter == 0)
  70.         load_zbuffer (n);
  71.     
  72.     zbuffer[n].zbufcounter -= zbuffer[n].zbufitemsize;
  73.     if (zbuffer[n].zbufcounter >= 0)
  74.       {
  75.         result = zbuffer[n].zbufp;
  76.         zbuffer[n].zbufp += zbuffer[n].zbufitemsize;
  77.         return (result);
  78.       }
  79.     else
  80.         return (NULL);
  81.   }
  82.  
  83.  
  84. /* function to load the nth zbuffer appropriately ... it resets the buffer
  85.  * pointers, etc. ... might as well give the user a chance to interrupt (in
  86.  * the Macintosh version) here, as long as we have to go to the disk anyway....
  87.  */
  88.  
  89. void load_zbuffer (n)
  90.   int n;
  91.   {
  92.     long nread;
  93.     void exit(), check_interrupt();
  94.  
  95. #ifdef LIGHTSPEED
  96.     nread = zbuffer[n].zbufsize;
  97.     FSRead (zbuffer[n].zbuffilep->refnum, &nread, zbuffer[n].zbufbase);
  98. #else
  99.     nread = fread (zbuffer[n].zbufbase, sizeof(char),
  100.             (int)zbuffer[n].zbufsize, zbuffer[n].zbuffilep);
  101. #endif
  102.  
  103.     zbuffer[n].zbufp = zbuffer[n].zbufbase;
  104.     zbuffer[n].zbufcounter = nread;
  105.     
  106.     if (ferror (zbuffer[n].zbuffilep))
  107.       {
  108.         printf ("\nFatal error in load_zbuffer while reading in data!\n");
  109.         printf ("(n=%d, nread=%ld)\n", n, nread);
  110.         exit (1);
  111.       }
  112.     
  113. #ifdef LIGHTSPEED
  114.     check_interrupt ();
  115. #endif
  116.   }
  117.  
  118.  
  119. /* function to return a pointer to the right place to store the next
  120.  * output item for the nth zbuffer ... when the buffer becomes full,
  121.  * it flushes it to disk, resets pointers, etc.
  122.  */
  123.  
  124. char *next_output_item (n)
  125.   int n;
  126.   {
  127.     char *result;
  128.     void flush_zbuffer();
  129.     
  130.     if (zbuffer[n].zbufcounter == zbuffer[n].zbufsize)
  131.         flush_zbuffer (n);
  132.     
  133.     result = zbuffer[n].zbufp;
  134.     zbuffer[n].zbufp += zbuffer[n].zbufitemsize;
  135.     zbuffer[n].zbufcounter += zbuffer[n].zbufitemsize;
  136.     return (result);
  137.   }
  138.  
  139.  
  140. /* function to flush out a zbuffer to the disk ... might as well give user
  141.  * a chance to interrupt here (in the Macintosh version), as long as we
  142.  * have to go to the disk anyway....
  143.  */
  144.  
  145. void flush_zbuffer (n)
  146.   int n;
  147.   {
  148.     long chars_written;
  149.     void exit();
  150.     
  151.     if (zbuffer[n].zbufcounter == 0)
  152.         return;
  153.  
  154. #ifdef LIGHTSPEED
  155.     chars_written = zbuffer[n].zbufcounter;
  156.     FSWrite (zbuffer[n].zbuffilep->refnum, &chars_written,
  157.                 zbuffer[n].zbufbase);
  158. #else
  159.     chars_written = fwrite (zbuffer[n].zbufbase, sizeof(char),
  160.                 (int)zbuffer[n].zbufcounter, zbuffer[n].zbuffilep);
  161. #endif
  162.     if (ferror(zbuffer[n].zbuffilep) || chars_written == 0)
  163.       {
  164.         printf ("\nFatal error in flush_zbuffer while writing out data!\n");
  165.         printf ("(n=%d, zbufcounter=%ld, chars_written=%ld)\n", n,
  166.                     zbuffer[n].zbufcounter, chars_written);
  167.         exit(1);
  168.       }
  169.     
  170.     zbuffer[n].zbufp = zbuffer[n].zbufbase;
  171.     zbuffer[n].zbufcounter = 0;
  172.  
  173. #ifdef LIGHTSPEED
  174.     check_interrupt ();
  175. #endif
  176.   }
  177.   
  178.  
  179.